//
//  GUIBattle.m
//  VicBattle
//
//  Created by Nguyen Nguyen on 5/3/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "BattleUILayer.h"

@implementation GUIBattle

+(CCScene *) scene
{
    CCScene *scene = [CCScene node];
    
    GUIBattle *battleUILayer = [GUIBattle node];
    [scene addChild:battleUILayer];
    
    return scene;
}

-(id) init
{
    if((self=[super init]))
    {
        CGSize winSize = [[CCDirector sharedDirector] winSize];
        
        battleLayer = [BUSBattle node];
        [self addChild:battleLayer];
        self.isTouchEnabled = YES;
        
        images = [NSArray arrayWithObjects:
                           @"archer.png",
                           @"knight.png",
                           @"ninja.png",
                           @"gunner.png",
                           @"healer.png",
                           @"soldier.png",
                           @"sorcerer.png",
                           nil];
        
        heroes = [[CCArray alloc] init];
        
        [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"heroes.plist"];
        CCSpriteBatchNode *batchHeroes = [CCSpriteBatchNode batchNodeWithFile:@"heroes.png"];
        
        [self addChild:batchHeroes];
        
        int x = winSize.width;
        for(int i = images.count - 1; i >= 0 ; --i)
        {
            NSString *image = [images objectAtIndex:i];
            CCSprite *sprite = [CCSprite spriteWithSpriteFrameName:image];
            x = x - 15 - sprite.textureRect.size.width;
            sprite.position = ccp(x, 30);
            [self addChild:sprite];
            [heroes addObject:sprite];
        }
        
        item = [CCSprite node];
        item.visible = NO;
        [self addChild:item];
    }
    return self;
}

-(void) registerWithTouchDispatcher
{
	[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}

- (void)selectSpriteForTouch:(CGPoint)touchLocation
{
    for(CCSprite *sprite in heroes)
    {
        if (CGRectContainsPoint(sprite.boundingBox, touchLocation)) 
        {
            item.visible = YES;
            item.position = sprite.position;
            [item setTexture:sprite.texture];
            [item setTextureRect:sprite.textureRect];
            break;
        }
    }
}

CGPoint oldPoint, newPoint;

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    oldPoint = [self convertTouchToNodeSpace:touch];
    
    [self selectSpriteForTouch:oldPoint];

    return YES;
}

-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    newPoint = [self convertTouchToNodeSpace:touch];
    
    if(item.visible == YES)
    {
        item.position = newPoint;
    }
    else
    {
        [battleLayer viewWithOlPoint:oldPoint andNewPoint:newPoint];
    }
    oldPoint = newPoint;
}

-(void) ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
    if(item.visible == YES)
    {
        int row, col;
        [battleLayer getRow:&row andCol:&col fromPoint:oldPoint];
        
        if(row != -1 && col != -1)
        {
            [battleLayer putCharacter:0 toRow:row toCol:col];
        }
    }
    
    item.visible = NO;
}

-(void) ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event
{
    item.visible = NO;
}

- (void) dealloc
{
    [heroes release];
    [super dealloc];
}

@end
